from os.path import join, abspath, dirname
from os import getcwd
import xlrd

class spreadsheet:
	#This is a manipultor for spreadsheet
	#beginning of class suite
	def __init__(self, name, head_row, last_col=255, last_row=65535):
		self.name=join(join(dirname(getcwd()), 'files'), name+'.xls')
		self.last_row=last_row
		self.last_col=last_col
		self.head_row=head_row
		
	def extract(self):
		"This function extracts rows from spreadsheet"
		#beginning of function suite
		
		book = xlrd.open_workbook(self.name)
		sheet = book.sheet_by_index(0)
		result=[]
		
		ceh_c1_name=''
		
		for row in range(self.head_row+1, self.last_row+1):
			try:
				sheet.cell_value(row, 0)
			except:
				pass
			else:
				temp=[]
				if len(sheet.cell_value(row, 1).strip())==0 and '0000' in sheet.cell_value(row+1, 1):
					ceh_c1_name=sheet.cell_value(row, 0)
				elif '0000' in sheet.cell_value(row, 1):
					temp+=[ceh_c1_name]
					for y in range(0, self.last_col):
						try:
							temp+=[sheet.cell_value(row, y)]
						except:
							pass
					result+=[temp]
		#end of function suite
		return result
		
	#def extract(self, filter=self.filter):
	#	"This function extracts rows from spreadsheet that satisfy filter"
	#	#beginning of function suite
	#	book = xlrd.open_workbook(self.name)
	#	sheet = book.sheet_by_index(0)
	#	result=[]
	#	for row in range(self.last_row, self.first_row, -1):
	#		try:
	#			test=True
	#			for f in filter:
	#				f_len=len(filter[f])
	#				if filter[f] not in sheet.cell_value(row, f):
	#					test=False
	#					break
	#			if test:
	#				temp=[]
	#				for y in range(0, self.col_len):
	#					temp+=[sheet.cell_value(row, y)]
	#				result+=[temp]
	#	#end of function suite
	#	return result